// 获取uuid
export const getUID = function() { // 获取唯一值
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random() * 16 | 0; var v = c === 'x' ? r : (r & 0x3 | 0x8)
    return v.toString(16)
  })
}

// 遍历对象，返回遍历后的新对象
export const objToObj = function(obj1, obj2) { // 获取唯一值
  for (const i in obj1) {
    if (obj2[i] !== null && obj2[i] !== '' && obj2[i] !== undefined) {
      obj1[i] = obj2[i]
    }
  }
}

export const getQueryString = function(name) {
  const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  const reg_rewrite = new RegExp('(^|/)' + name + '/([^/]*)(/|$)', 'i')
  const r = window.location.search.substr(1).match(reg)
  const q = window.location.pathname.substr(1).match(reg_rewrite)
  debugger
  if (r != null) {
    return unescape(r[2])
  } else if (q != null) {
    return unescape(q[2])
  } else {
    return null
  }
}

/**
 * 二维树状结构转树形树状结构
 */
/**
 * 将通用的二维数据结构转换为树状数据结构
 * @param  {String} rootParentIdValue 表示根节点的父类id值
 * @param  {String} parentIdName      表示父类id的节点名称
 * @param  {String} nodeIdName        表示二维结构中，每个对象主键的名称
 * @param  {Array} listData           为二维结构的数据
 * @return {Array}                    转换后的tree结构数据
 */
export function treeData(rootParentIdValue, parentIdName, nodeIdName, listData) {
  if (!(listData instanceof Array && listData.length > 0)) {
    throw new Error('格式不正确，无法转换')
  } else {
    var rootList = []
    var nodeList = []
    listData.forEach(function(node) {
      if (node[nodeIdName] === rootParentIdValue) {
        rootList.push(node)
      } else {
        nodeList.push(node)
      }
    })
    if (nodeList.length > 0 && rootList.length > 0) {
      childrenNodeAdd(rootList, nodeList)
      return rootList
    } else if (rootList.length > 0) {
      throw new Error('没有对应的子节点集合')
    } else {
      throw new Error('没有对应的父类节点集合')
    }
    // eslint-disable-next-line no-inner-declarations
    function childrenNodeAdd(rootNodeList, childrenList) {
      if (childrenList.length > 0) {
        rootNodeList.forEach(function(rootNode) {
          rootNode['children'] = []
          childrenList.forEach(function(childrenNode, childrenIndex) {
            if (parentIdName in childrenNode && rootNode[nodeIdName] === childrenNode[parentIdName]) {
              rootNode['children'].push(childrenNode)
            }
          })
          childrenNodeAdd(rootNode['children'], childrenList)
        })
      }
    }
  }
}

/**
 * 根据身份证获取年龄和性别
 * @param UUserCard
 */
export function getAgeAdSex(identityCard) {
  const len = (identityCard + '').length
  if (len === 0) {
    return {}
  } else {
    if ((len !== 15) && (len !== 18)) {
      return {}
    }
  }
  let strBirthday = ''
  let strSex = ''
  if (len === 18) {
    strBirthday = identityCard.substr(6, 4) + '/' + identityCard.substr(10, 2) + '/' + identityCard.substr(12, 2)
    strSex = identityCard.substr(16, 1)
  }
  if (len === 15) {
    strBirthday = '19' + identityCard.substr(6, 2) + '/' + identityCard.substr(8, 2) + '/' + identityCard.substr(10, 2)
    strSex = identityCard.substr(14, 1)
  }
  strSex = strSex * 1 % 2
  if (strSex === 1) {
    strSex = 1
  } else if (strSex === 0) {
    strSex = 2
  }
  // 时间字符串里，必须是“/”
  const birthDate = new Date(strBirthday)
  const nowDateTime = new Date()
  let age = nowDateTime.getFullYear() - birthDate.getFullYear()
  // 再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
  if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() === birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
    age--
  }
  return {
    age: age,
    sex: strSex
  }
}

/**
 * 深拷贝
 * @param obj
 * @returns {any}
 */
export function deepClone(obj) {
  const _obj = JSON.stringify(obj)
  const objClone = JSON.parse(_obj)
  return objClone
}

/**
 * 数组转字符，以 str 隔开
 * @param arr
 */
export function arrToStr(arr, str = '-') {
  let _str = ''
  if (arr instanceof Array) {
    _str = arr.join(str)
  } else {
    _str = arr
  }
  return _str
}

/**
 * 字符转数组，以 str 为标识
 * @param arr
 */
export function strToArr(arr, str = '-') {
  let _arr = ''
  if ((typeof arr === 'string') && arr.constructor === String) {
    _arr = arr.split(str)
  } else {
    _arr = arr
  }
  return _arr
}

